//
//  CLReadController+Operation.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/31.
//

#import "CLReadController+Operation.h"
#import "CLReadConfigure.h"
#import "CLReadLongPressViewController.h"
#import "CLReadTextFastParser.h"
#import "CLReadController+EffectType.h"
@implementation CLReadController (Operation)

/// 获取指定阅读记录阅读页
- (CLReadViewController *)getReadViewController:(CLReadRecordModel *)recordModel {
    if (recordModel != nil) {
        if ([CLReadConfigure sharedInstance].openLongPress) { // 需要返回支持长按的控制器
            CLReadLongPressViewController *controller = [[CLReadLongPressViewController alloc]init];
            controller.viewModel.prefersNavigationBarHidden = YES;
            controller.recordModel = recordModel;
            
            controller.readModel = self.readModel;
            
            return controller;
            
        }else{ // 无长按功能
            
            CLReadViewController *controller = [[CLReadViewController alloc]init];
            
            controller.recordModel = recordModel;
            
            controller.readModel = self.readModel;
            
            return controller;
        }
    }
    return nil;
}
    
/// 获取当前阅读记录阅读页
- (CLReadViewController *)getCurrentReadViewController:(BOOL)isUpdateFont {
    
    if ([CLReadConfigure sharedInstance].effectType != CLEffectTypeScroll) { // 滚动模式不需要创建
        if (isUpdateFont) { [self.readModel.recordModel updateFont:YES]; }
        
        return [self getReadViewController:[self.readModel.recordModel copyModel]];
    }
    
    return nil;
}

/// 获取上一页控制器
- (UIViewController *)getAboveReadViewController {

    CLReadRecordModel *recordModel = [self getAboveReadRecordModel:self.readModel.recordModel];

    if (recordModel == nil) { return nil; }

    return [self getReadViewController:recordModel];
}

/// 获取仿真模式背面(只用于仿真模式背面显示)
- (CLReadViewBGController *)getReadViewBGController:(CLReadRecordModel *)recordModel {
    return [self getReadViewBGController:recordModel targetView:nil];
}

- (CLReadViewBGController *)getReadViewBGController:(CLReadRecordModel *)recordModel targetView:(UIView * __nullable)targetView {
    
    CLReadViewBGController *vc = [[CLReadViewBGController alloc]init];
    
    vc.recordModel = recordModel;
    
    UIView *otherTargetView = targetView ? targetView : [self getReadViewController: recordModel].view;
    
    vc.targetView = otherTargetView;
    
    return vc;
}


/// 获取下一页控制器
- (UIViewController *)getBelowReadViewController {
    
    CLReadRecordModel *recordModel = [self getBelowReadRecordModel: self.readModel.recordModel];
    
    if (recordModel == nil) { return nil; }
    
    return [self getReadViewController:recordModel];
}

/// 跳转指定章节(指定页面)
- (void)goToChapter:(NSString *)chapterID {
    [self goToChapter:chapterID toPage:0];
}
- (void)goToChapter:(NSString *)chapterID toPage:(NSInteger)toPage {

    [self goToChapter:chapterID number:toPage isLocation: NO];
}

/// 跳转指定章节(指定坐标)
- (void)goToChapter:(NSString *)chapterID location:(NSInteger)location {

    [self goToChapter: chapterID number: location isLocation: YES];
}

/// 跳转指定章节 number:页码或者坐标 isLocation:是页码(false)还是坐标(true)
- (void)goToChapter:(NSString *)chapterID number:(NSInteger)number isLocation:(BOOL)isLocation {

    // 复制阅读记录
    CLReadRecordModel *recordModel = [self.readModel.recordModel copyModel];
    
    // 书籍ID
    NSString *bookID = recordModel.bookid;

        // 检查是否存在章节内容
    BOOL isExist = [CLReadChapterModel isExist:bookID chapterid:chapterID];

        // 存在 || 不存在(但是为本地阅读)
    if (isExist || self.readModel.bookSourceType == BookSourceTypeLocal) {
        
        if (!isExist) {
            
            // 获取章节数据
            [CLReadTextFastParser parser:self.readModel chapterid:chapterID];
        }
        
        if (isLocation) {
            
            // 坐标定位
            [recordModel modify:chapterID location:number isSave:NO];
            
        }else{
            
            // 分页定位
            [recordModel modify:chapterID toPage:number isSave:NO];
        }
        
        // 阅读阅读记录
        [self updateReadRecordWithRecordModel:recordModel];
        
        
        // 展示阅读
        [self creatPageController:[self getReadViewController:recordModel]];
        
    }else{ // 加载网络章节数据

                // ----- 搜索网络小说 -----

//            MBProgressHUD.showLoading(view)
//
//            // 加载章节数据
//            NJHTTPTool.request_novel_read(bookID, chapterID) { [weak self] (type, response, error) in
//
//                MBProgressHUD.hide(self?.view)
//
//                if type == .success {
//
//                    // 获取章节数据
//                    let data = HTTP_RESPONSE_DATA_DICT(response)
//
//                    // 解析章节数据
//                    let chapterModel = DZMReadChapterModel(data)
//
//                    // 章节类容需要进行排版一篇
//                    chapterModel.content = DZMReadParser.contentTypesetting(content: chapterModel.content)
//
//                    // 保存
//                    chapterModel.save()
//
//                    // 修改阅读记录
//                    recordModel.modify(chapterID: chapterModel.chapterID, toPage: toPage, isSave: false)
//
//                    // 更新阅读记录
//                    self?.updateReadRecord(recordModel: recordModel)
//
//                    // 展示阅读记录
//                    self?.creatPageController(displayController: GetReadViewController(recordModel: recordModel))
//                }
//            }
        }

        // ----- 搜索网络小说 -----

//        // 预加载下一章(可选)
//        if readModel.bookSourceType == .network { // 网络小说
//
//            if !recordModel.isLastChapter && !DZMReadChapterModel.isExist(bookID: bookID, chapterID: chapterID) {
//
//                // 加载章节数据
//                NJHTTPTool.request_novel_read(bookID, chapterID) { [weak self] (type, response, error) in
//
//                    if type == .success {
//
//                        // 获取章节数据
//                        let data = HTTP_RESPONSE_DATA_DICT(response)
//
//                        // 解析章节数据
//                        let chapterModel = DZMReadChapterModel(data)
//
//                        // 章节类容需要进行排版一篇
//                        chapterModel.content = DZMReadParser.contentTypesetting(content: chapterModel.content)
//
//                        // 保存
//                        chapterModel.save()
//                    }
//                }
//            }
//        }
}
//
/// 获取当前记录上一页阅读记录
- (CLReadRecordModel *)getAboveReadRecordModel:(CLReadRecordModel *)otherRecordModel {

    // 阅读记录为空
    if (otherRecordModel.chapterModel == nil) { return nil; }
    
    // 复制
    CLReadRecordModel *recordModel = [otherRecordModel copyModel];
    
    // 书籍ID
    NSString *bookID = recordModel.bookid;
    
    // 章节ID
    NSString *chapterID = recordModel.chapterModel.previousChapterID;
    
    // 第一章 第一页
    if (recordModel.isFirstChapter && recordModel.isFirstPage){
        
        NSLog(@"已经是第一页了");
        
        return nil;
    }

        // 第一页
    if (recordModel.isFirstPage) {
        
        // 检查是否存在章节内容
        BOOL isExist = [CLReadChapterModel isExist:bookID chapterid:chapterID];
        
        // 存在 || 不存在(但是为本地阅读)
        if (isExist || self.readModel.bookSourceType == BookSourceTypeLocal) {
            
            // 获取章节数据
            
            if (!isExist) { [CLReadTextFastParser parser:self.readModel chapterid:chapterID]; }
            
            // 修改阅读记录
            [recordModel modify:chapterID toPage:CL_READ_LAST_PAGE isSave:NO];
            
        }else{ // 加载网络章节数据
            
    // ----- 搜索网络小说 -----
    
    //                MBProgressHUD.showLoading(view)
    //
    //                // 加载章节数据
    //                NJHTTPTool.request_novel_read(bookID, chapterID) { [weak self] (type, response, error) in
    //
    //                    MBProgressHUD.hide(self?.view)
    //
    //                    if type == .success {
    //
    //                        // 获取章节数据
    //                        let data = HTTP_RESPONSE_DATA_DICT(response)
    //
    //                        // 解析章节数据
    //                        let chapterModel = DZMReadChapterModel(data)
    //
    //                        // 章节类容需要进行排版一篇
    //                        chapterModel.content = DZMReadParser.contentTypesetting(content: chapterModel.content)
    //
    //                        // 保存
    //                        chapterModel.save()
    //
    //                        // 修改阅读记录
    //                        recordModel.modify(chapterID: chapterModel.chapterID, toPage: DZM_READ_LAST_PAGE, isSave: false)
    //
    //                        // 更新阅读记录
    //                        self?.updateReadRecord(recordModel: recordModel)
    //
    //                        // 展示阅读记录
    //                        self?.setViewController(displayController: GetReadViewController(recordModel: recordModel), isAbove: true, animated: true)
    //                    }
    //                }
            
            return nil;
        }
        
    }else{ [recordModel previousPage]; }
    
    // ----- 搜索网络小说 -----
    
    // 预加载上一章(可选)(一般上一章就要他自己拉一下加载吧,看需求而定,上下滚动模式的就会提前加载好上下章节)
    
    return recordModel;
}

/// 获取当前记录下一页阅读记录
- (CLReadRecordModel *)getBelowReadRecordModel:(CLReadRecordModel *)t_recordModel {
    // 阅读记录为空
     if (t_recordModel.chapterModel == nil) { return nil; }
    
    // 复制
    CLReadRecordModel *recordModel = [t_recordModel copyModel];
    
    // 书籍ID
    NSString *bookID = recordModel.bookid;
    
    // 章节ID
    NSString *chapterID = recordModel.chapterModel.nextChapterID;
    
    // 最后一章 最后一页
    if (recordModel.isLastChapter && recordModel.isLastPage) {
        
        NSLog(@"已经是最后一页了");
        
        return nil;
    }
    
    // 最后一页
    if (recordModel.isLastPage) {
        
        // 检查是否存在章节内容
        BOOL isExist = [CLReadChapterModel isExist:bookID chapterid:chapterID];
        
        // 存在 || 不存在(但是为本地阅读)
        if (isExist || self.readModel.bookSourceType == BookSourceTypeLocal) {
            
            // 获取章节数据
            if (!isExist) {
                [CLReadTextFastParser parser:self.readModel chapterid:chapterID];
            }
            
            // 修改阅读记录
            [recordModel modify:chapterID toPage:0 isSave:NO];
            
        }else{ // 加载网络章节数据
            
// ----- 搜索网络小说 -----

//                MBProgressHUD.showLoading(view)
//
//                // 加载章节数据
//                NJHTTPTool.request_novel_read(bookID, chapterID) { [weak self] (type, response, error) in
//
//                    MBProgressHUD.hide(self?.view)
//
//                    if type == .success {
//
//                        // 获取章节数据
//                        let data = HTTP_RESPONSE_DATA_DICT(response)
//
//                        // 解析章节数据
//                        let chapterModel = DZMReadChapterModel(data)
//
//                        // 章节类容需要进行排版一篇
//                        chapterModel.content = DZMReadParser.contentTypesetting(content: chapterModel.content)
//
//                        // 保存
//                        chapterModel.save()
//
//                        // 修改阅读记录
//                        recordModel.modify(chapterID: chapterModel.chapterID, toPage: 0, isSave: false)
//
//                        // 更新阅读记录
//                        self?.updateReadRecord(recordModel: recordModel)
//
//                        // 展示阅读记录
//                        self?.setViewController(displayController: GetReadViewController(recordModel: recordModel), isAbove: false, animated: true)
//                    }
//                }
//
//                return nil
        }
        
    } else {
        [recordModel nextPage];
    }
    
// ----- 搜索网络小说 -----

//        // 预加载下一章(可选)
//        if readModel.bookSourceType == .network { // 网络小说
//
//            if !recordModel.isLastChapter && !DZMReadChapterModel.isExist(bookID: bookID, chapterID: chapterID) {
//
//                // 加载章节数据
//                NJHTTPTool.request_novel_read(bookID, chapterID) { [weak self] (type, response, error) in
//
//                    if type == .success {
//
//                        // 获取章节数据
//                        let data = HTTP_RESPONSE_DATA_DICT(response)
//
//                        // 解析章节数据
//                        let chapterModel = DZMReadChapterModel(data)
//
//                        // 章节类容需要进行排版一篇
//                        chapterModel.content = DZMReadParser.contentTypesetting(content: chapterModel.content)
//
//                        // 保存
//                        chapterModel.save()
//                    }
//                }
//            }
//        }
    return recordModel;
}
//
/// 更新阅读记录(左右翻页模式)
- (void)updateReadRecord:(CLReadViewController *)controller {
    [self updateReadRecordWithRecordModel:controller.recordModel];
}

/// 更新阅读记录(左右翻页模式)
- (void)updateReadRecordWithRecordModel:(CLReadRecordModel *)recordModel {

    if (recordModel != nil) {

        self.readModel.recordModel = recordModel;

        [self.readModel.recordModel save];

        CL_READ_RECORD_CURRENT_CHAPTER_LOCATION = recordModel.locationFirst;
    }
}
@end
